﻿using WorkOrder.Model;
namespace WorkOrder.Business
{
    public class Business_Table_Department
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Department model)
        {
            string sqlstr = " insert into Table_Department (dept_name,remark) values(";
            sqlstr += "'" + model.Dept_name + "','" + model.Remark + "')";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 更新
        public bool Update(Model_Table_Department model)
        {
            string sql = "update Table_Department set dept_name='" + model.Dept_name
                + "', remark='" + model.Remark + "' where dept_id=" + model.Dept_id;
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 检查记录是否被引用
        /// <summary>
        /// 检查记录是否被引用
        /// </summary>
        /// <returns>true：已被引用，false：未被引用</returns>
        public bool hasUsed(string keyValue)
        {
            string sql = "select top 1 order_state_id from Table_Work_order where order_state_id=" + keyValue;
            return dbhelper.ExecuteScalar(sql) != null;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Department GetData(string keyValue)
        {
            string sql = "select * from Table_Department where dept_id=" + keyValue;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Department tmp;
                    tmp = new Model_Table_Department(rdr["dept_id"].ToString(), rdr["dept_name"].ToString(),
                        rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        {
            string sql = "UP_GET_PAGEINFO 'Table_Department','*"
                     + "','" + condition + "','','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
            return dbhelper.Query(sql);
        }
        public System.Data.DataSet GetDatas(string condition)
        {
            string sqlstr = "select * from Table_Department where " + condition;
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 删除记录
        public bool Delete(string fieldName, string fieldValue)
        {
            string sqlstr = "delete Table_Department where " + fieldName + "=" + fieldValue;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion
    }
}
